﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WQMS.Data
{
    public static class DbContext
    {
        public static SqlSugarClient GetDbContext
        {
            get => new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Server=.;Database=WQMSDB;User ID=sa;Password=123456;",//必填, 数据库连接字符串
                DbType = DbType.SqlServer,         //必填, 数据库类型
                IsAutoCloseConnection = true,      //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                InitKeyType = InitKeyType.SystemTable    //默认SystemTable, 字段信息读取, 如：该属性是不是主键，是不是标识列等等信息
            });
        }
        /// <summary>
        /// 表生成实体
        /// </summary>
        /// <param name="strPath">实体存放路径</param>
        /// <param name="tbNames">生成实体的表名，为空时生成所有表</param>
        public static void CreateClassFileByDBTalbe(string strPath, string[] tbNames = null)
        {
            if (tbNames == null)
            {
                GetDbContext.DbFirst.IsCreateAttribute().CreateClassFile(strPath, "WQMS.Entity");
            }
            else
            {
                GetDbContext.DbFirst.Where(tbNames).IsCreateAttribute().CreateClassFile(strPath, "WQMS.Entity");
            }
        }

    }
}
